//
//  ScrollViewAddTableViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/4/26.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "ScrollViewAddTableViewController.h"

#import "ContentScrollView.h"
#import "BottomScrollView.h"
#import "GGAddtion.h"



@interface ScrollViewAddTableViewController ()<UIScrollViewDelegate>
{
     BOOL canMove;
}
@property (nonatomic, weak)ContentScrollView *scrollView;

@end

@implementation ScrollViewAddTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(canMove) name:@"OtherCanMove" object:nil];
    [self setUpUI];
    canMove = YES;  // 最开始的时候是可以滑动的
    [self refreshView];
}

- (void)canMove{
    canMove = YES;
}

- (void)setUpUI{
    self.view.backgroundColor = [UIColor whiteColor];
    // scrollView
    ContentScrollView * scrollView = [[ContentScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    
//    scrollView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    
    // scrollView上方的视图
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    [scrollView addSubview:topView];
    topView.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.4];
    
    // scrollView下方包含有TableView的视图
    BottomScrollView * bottomView = [[BottomScrollView alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];
    [scrollView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {   // 这样布局scrollView就能滑动了
        make.width.equalTo(scrollView);
        make.height.mas_equalTo([UIScreen mainScreen].bounds.size.height);
        make.left.right.bottom.equalTo(scrollView);
        make.top.equalTo(topView.mas_bottom);
    }];
}
#pragma mark -
- (void)refreshView{
    if(@available(iOS 11.0,*)){
        UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
        //配置信息
        refreshControl.tintColor = [UIColor brownColor];
        NSDictionary *attribudes = @{NSForegroundColorAttributeName:[UIColor redColor]};
        refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新" attributes:attribudes];
        //添加事件
        [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
        self.scrollView.refreshControl = refreshControl;
    }
}

#pragma mark - refresh
- (void)refresh:(UIRefreshControl *)sender{
    //发送通知告诉tableView需要刷新了
    [[NSNotificationCenter defaultCenter] postNotificationName:@"needRefresh" object:nil];
    [sender endRefreshing];
}
#pragma mark - UIScrollViewDelegate  (核心代码)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat contentOffsetY = scrollView.contentOffset.y;
//    NSLog(@"scrollView的偏移量：===%f", contentOffsetY);
//    CGFloat maxOffsetY = 200 - 64;  // 简单的代码，没有适配iphoneX
    //
    CGFloat maxOffsetY = 200;
    //上拉
    if (contentOffsetY > maxOffsetY) {
        [scrollView setContentOffset:CGPointMake(0, maxOffsetY)]; //  设置最大偏移
        [[NSNotificationCenter defaultCenter] postNotificationName:@"contentCanMove" object:nil]; // 告诉底部内容视图能进行滑动了
        canMove = NO;   // 自己不能滑动了
    }
    //下拉
    if(contentOffsetY < 0){
        
    }
    if (canMove == NO) {
        [scrollView setContentOffset:CGPointMake(0, maxOffsetY)];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
